package com.imooc.juc;

import com.imooc.juc.bean.SaleTicket;
import com.imooc.juc.bean.SaleTicketLock;

/**
 * @Date:2021/11/5/21:50
 * @Description:java8新特性，用 Lock 代替了 synchronized
 */
public class ProconsumerLockDemo {
    public static void main(String[] args) {
        com.imooc.juc.bean.SaleTicketLock saleTicket = new SaleTicketLock();

        new Thread( ()->{
            try {
                for (int i = 0; i < 10; i++) {
                    saleTicket.increment();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        },"A" ).start();

        new Thread( ()->{
            try {
                for (int i = 0; i <10; i++) {
                    saleTicket.decrement();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"B" ).start();

        new Thread( ()->{
            try {
                for (int i = 0; i < 10; i++) {
                    saleTicket.increment();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        },"c" ).start();

        new Thread( ()->{
            try {
                for (int i = 0; i <10; i++) {
                    saleTicket.decrement();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        },"D" ).start();

    }
}
